#include<iostream>
using namespace std;
#include"stdTcpService.h"
#define PORT 8080
#define BUFFER_SIZE 32
#include<json-c/json.h>
void registerFuc(StdTcpSocket& client)
{
        char username[BUFFER_SIZE];
        cout<<"请输入用户名：";
        scanf("%s",username);
        
        char passwd[BUFFER_SIZE];
        cout<<"请输入密码：";
        scanf("%s",passwd);
        /*创建json对象*/
        json_object *jsonObj=json_object_new_object(); 
        json_object_object_add(jsonObj,"type",json_object_new_int(1));
        json_object_object_add(jsonObj,"username",json_object_new_string(username));
        json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));

        /*将json对象转换成字符串*/
        const char *jsonStr=json_object_to_json_string(jsonObj);

        // Msg msg;

        // /*发送信息到服务器*/

        client.sendMessage(jsonStr);

        /*接受服务端的信息*/
        string recvData;
        client.receMessage(recvData);
        std::cout<<"recvData:"<<recvData<<std::endl;

        return;


}
void loginFunc(StdTcpSocket& client)
{
        char username[BUFFER_SIZE];
        cout<<"请输入用户名：";
        scanf("%s",username);
        
        char passwd[BUFFER_SIZE];
        cout<<"请输入密码：";
        scanf("%s",passwd);
        /*创建json对象*/
        json_object *jsonObj=json_object_new_object(); 
        json_object_object_add(jsonObj,"type",json_object_new_int(2));
        json_object_object_add(jsonObj,"username",json_object_new_string(username));
        json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));

        /*将json对象转换成字符串*/
        const char *jsonStr=json_object_to_json_string(jsonObj);

        // Msg msg;

        // /*发送信息到服务器*/

        client.sendMessage(jsonStr);

        /*接受服务端的信息*/
        string recvData;
        client.receMessage(recvData);
        std::cout<<"recvData:"<<recvData<<std::endl;

        return;


}
 
 


int main()
{
    StdTcpSocket client;

    client.conectToServer("192.168.111.128",PORT);

    while(1)
    {
      int choice;
      cin>>choice;
      if(choice==1)
      {
        registerFuc(client);
      }
      else if(choice==2)
      {
        loginFunc(client);
      }
      
    }
    return 0;

}